package com.zz.utils;

import com.zz.constants.ResultCodeEnum;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 全局统一返回结果类
 */
@Data
@ApiModel(value = "全局统一返回结果")
@Slf4j
public class ResultUtil<T> {

    private Integer code;

    private String message;

    private T data;

    ResultUtil(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    ResultUtil(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    ResultUtil(T data) {
        this.data = data;
    }

    ResultUtil(T data, String message) {
        this.data = data;
        this.message = message;
    }

    public ResultUtil() {
    }

    // 成功
    public static <T> ResultUtil<T> success() {
        return new ResultUtil<>(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage());
    }

    // 成功
    public static <T> ResultUtil<T> success(T data) {
        return new ResultUtil<T>(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(), data);
    }

    // 成功
    public static <T> ResultUtil<T> success(String message, T data) {
        return new ResultUtil<>(ResultCodeEnum.SUCCESS.getCode(), message, data);
    }

    // 成功
    public static <T> ResultUtil<T> success(Integer code, String message, T data) {
        return new ResultUtil<T>(code, message, data);
    }

    // 失败-返回未知错误
    public static <T> ResultUtil<T> error() {
        return new ResultUtil<>(ResultCodeEnum.UNKNOWN_ERROR.getCode(), ResultCodeEnum.UNKNOWN_ERROR.getMessage());
    }

    // 失败
    public static <T> ResultUtil<T> error(String message) {
        log.error(message);
        return new ResultUtil<>(ResultCodeEnum.UNKNOWN_ERROR.getCode(), message);
    }

    // 失败
    public static <T> ResultUtil<T> error(Integer code, String message) {
        return new ResultUtil<>(code, message);
    }

    // 设置结果，形参为结果枚举
    public static <T> ResultUtil<T> setResult(ResultCodeEnum result) {
        return new ResultUtil<>(result.getCode(), result.getMessage());
    }


}
